package com.hdoit.ares.web.app.apis;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.acc.model.WithdrawParam;
import com.hdoit.ares.logical.acc.service.IWithdrawParamService;
import com.hdoit.ares.logical.app.model.AppRemindMessage;
import com.hdoit.ares.logical.app.service.IAppRemindMessageService;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.component.webapi.BaseApi;
import com.tennetcn.component.webapi.ResponseModel;
import com.tennetcn.component.webapi.ResponseStatus;

@Controller
@RequestMapping(value="/api/v1/appRemindMessageAres/",method = {RequestMethod.GET,RequestMethod.POST},produces="application/json;charset=utf-8")
@Api(value="/api/v1/appRemindMessageAres/")
@CrossOrigin(origins = "*",maxAge = 3600)
public class AppRemindMessageAresApi extends BaseApi{

	private IAppRemindMessageService appRemindMessageService;
	private IWithdrawParamService withdrawParamService;
	
	public IWithdrawParamService getWithdrawParamService() {
		return withdrawParamService;
	}

	@Resource
	public void setWithdrawParamService(IWithdrawParamService withdrawParamService) {
		this.withdrawParamService = withdrawParamService;
	}

	public IAppRemindMessageService getAppRemindMessageService() {
		return appRemindMessageService;
	}

	@Resource
	public void setAppRemindMessageService(
			IAppRemindMessageService appRemindMessageService) {
		this.appRemindMessageService = appRemindMessageService;
	}
	
	private final String messageListApi="messageList";
	private final String updateRemindMessageApi="updateRemindMessage";
	private final String deleteMessageApi="deleteMessage";
	private final String unReadCountApi="unReadCount";
	
	@ResponseBody
    @RequestMapping(value = messageListApi)
    @ApiOperation(notes = messageListApi, httpMethod = "POST", value = "分页查询所有信息列表")
	public String messageList(@ApiParam("第几页,必选") @RequestParam(value="pageNum",required=true)Integer pageNum){
		
		ResponseModel rm=new ResponseModel();
		String custId = getLoginId();
		if(pageNum<1){
			pageNum=null;
			rm.setMessage("请输入正确的页数");
		}
		WithdrawParam wp = withdrawParamService.queryModelByHelpCode("AppMessagePageSize");
		Integer length = wp.getParam();
		List<AppRemindMessage> appRemindMessages=appRemindMessageService.queryMessageList(custId, pageNum, length);
		
		if(appRemindMessages.size()>0){
			rm.setStatus(ResponseStatus.success);
			rm.setMessage("ok");
			List<String> params = new ArrayList<String>();
			Field[] fields = AppRemindMessage.class.getDeclaredFields();
			for(Field field : fields){
				params.add(field.getName());
			}
			
			rm.putList("appRemindMessage", appRemindMessages, params);
		}else{
			rm.setMessage("没有查询到数据");
		}
		
		return rm.toJson();
	}
	
	@ResponseBody
	@RequestMapping(value = updateRemindMessageApi)
    @ApiOperation(notes = updateRemindMessageApi, httpMethod = "POST", value = "修改信息的读取状态")
	public String updateRemindMessage(String id){
		
		ResponseModel rm = new ResponseModel();
		
		AppRemindMessage appRemindMessage = new AppRemindMessage();
		appRemindMessage = appRemindMessageService.queryModel(id);
		if(appRemindMessage != null){
			appRemindMessage.setMessageStatus("read");
			appRemindMessage.setModelStatus(ModelStatus.update);
			boolean result=appRemindMessageService.applyChange(appRemindMessage);
			if(result == true){
				rm.setStatus(ResponseStatus.success);
				rm.setMessage("读取状态修改成功");
			}else{
				rm.setMessage("读取状态修改失败");
			}
		}
		
		return rm.toJson();
	}
	
	@ResponseBody
	@RequestMapping(value = deleteMessageApi)
    @ApiOperation(notes = deleteMessageApi, httpMethod = "POST", value = "删除选中的信息")
	public String deleteMessage(String id){
		
		ResponseModel rm = new ResponseModel();
		AppRemindMessage appRemindMessage = new AppRemindMessage();
		appRemindMessage = appRemindMessageService.queryModel(id);
		if(appRemindMessage != null){
			boolean resutl = appRemindMessageService.deleteModel(id);
			if(resutl == true){
				rm.setStatus(ResponseStatus.success);
				rm.setMessage("消息删除成功");
			}else{
				rm.setMessage("消息删除失败");
			}
		}
		
		return rm.toJson();
	}
	
	@ResponseBody
	@RequestMapping(value = unReadCountApi)
    @ApiOperation(notes = unReadCountApi, httpMethod = "POST", value = "未读消息的数量")
	public String unReadCount(){
		
		ResponseModel rm = new ResponseModel();
		String custId = getLoginId();
		int result = appRemindMessageService.selectUnReadCount(custId);
		if(result >= 0){
				rm.setStatus(ResponseStatus.success);
				rm.setMessage("未读消息的数量为"+result);
				rm.put("result", result);
			}else{
				rm.setMessage("查询失败");
			}

		return rm.toJson();
	}
}
